Get the size of an object in bytesΒΆ

Get the size of an object in bytes.
import sys

str1 = "one"
str2 = "four"
str3 = "three"

print("Memory size of '" + str1 + "' = " + str(sys.getsizeof(str1)) + " bytes")
print("Memory size of '" + str2 + "' = " + str(sys.getsizeof(str2)) + " bytes")
print("Memory size of '" + str3 + "' = " + str(sys.getsizeof(str3)) + " bytes")

Output:

Memory size of 'one' = 52 bytes
Memory size of 'four' = 53 bytes
Memory size of 'three' = 54 bytes